<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>角色操作模块页面</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/res/layui/css/layui.css" media="all">
    <script src="/res/js/jquery.js"></script>

</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>角色操作模块页面</legend>
</fieldset>
<form class="layui-form" action="" lay-filter="example">
    <input type="hidden" name="roleId" id="roleId"><!-- 加一个隐藏主键 -->
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">角色NAME:</label>
                <div class="layui-input-block">
                    <input type="text" name="roleName" id="roleName"  placeholder="Name" autocomplete="off" class="layui-input">
                </div>
            </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">角色备注:</label>
            <div class="layui-input-block">
                <input type="text" name="roleRemark" id="roleRemark"  placeholder="备注" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">选择模块</label>
        <div class="layui-input-inline">
            <div id="test12" class="demo-tree-more"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
            <button type="button" class="layui-btn layui-btn-primary" id="close_btn">关闭</button>
        </div>
    </div>
</form>

<script src="/res/layui/layui.js"></script>


<script>


    var tree;
    layui.use(['form', 'layedit', 'laydate', 'jquery', 'tree','util'], function () {


        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            ,util = layui.util
            , $ = layui.$;
        tree = layui.tree;


        $.post("/logisticsRole/showData","",function (resp){
            if (resp.state == 1) {
                console.info(resp.data);
                tree.render({
                    elem: '#test12'
                    ,data: resp.data
                    ,showCheckbox: true  //是否显示复选框
                    ,id: 'demoId1'
                    ,isJump: true //是否允许点击节点时弹出新窗口跳转
                    ,click: function(obj) {
                        var data = obj.data;  //获取当前点击的节点数据
                    }
                });


                /**
                 * 当点击编辑按钮时会传入一个id到form。html页面
                 */
                let id = getQueryString("id");
                if (id) {
                    let url = "/logisticsRole/show";
                    let data_ = {id: id};
                    $.post(url, data_, function (resp) {
                        console.info(resp)
                        if (resp.state == 1) {
                            $("#roleId").val(resp.data.data1.roleId);
                            $("#roleName").val(resp.data.data1.roleName);
                            $("#roleRemark").val(resp.data.data1.roleRemark);
                            for (let i = 0; i <resp.data.data2.length ; i++) {
                                tree.setChecked('demoId1',resp.data.data2[i].modelId);
                            }
                            form.render();
                        }
                    }, "json");
                }
            }
        },"json");


        //监听提交
        form.on('submit(demo1)', function (data) {
            let data_ = data.field;
            let spCodesTemp = "";
            $('input:checkbox[name="name"]:checked').each(function (i,val) {
                if (0 == i) {
                    spCodesTemp = $(this).val();
                    console.info($(this).next().val());
                } else {
                    spCodesTemp += ("," + $(this).val());
                }
            });
            console.info(spCodesTemp);
            data_.name = spCodesTemp;
            let url = "/logisticsRole/addAndUpdate";
            $.post(url, data_, function (resp) {
                if (resp.state == 1) {
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                    parent.table.reload('dataLXList');
                } else {
                    layer.msg(resp.mgs);
                }

            }, "json");
            return false;
        });

        /**
         * 得到地址上得值
         * @param id
         * @returns {string|null}
         */
        function getQueryString(id) {
            var reg = new RegExp("(^|&)" + id + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]);
            return null;
            console.info(r);
        }

        $(function () {
            $("#close_btn").on("click",function () {
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        });


    });
</script>
</body>
</html>